package models

import (
	"awesome/config"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Name     string `json:"name" gorm:"not null"`
	Email    string `json:"email" gorm:"unique;not null"`
	Password string `json:"password" gorm:"not null"`
}

// Create a new user
func CreateUser(user *User) error {
	println(config.DB)
	return config.DB.Create(user).Error
}

// Get a user by ID
func GetUserByID(id uint) (*User, error) {
	var user User
	err := config.DB.First(&user, id).Error
	if err != nil {
		return nil, err
	}
	return &user, nil
}

// Get a user by email
func GetUserByEmail(email string) (*User, error) {
	var user User
	err := config.DB.Where("email = ?", email).First(&user).Error
	if err != nil {
		return nil, err
	}
	return &user, nil
}

// Update a user
func UpdateUser(user *User) error {
	return config.DB.Save(user).Error
}

// Delete a user
func DeleteUser(user *User) error {
	return config.DB.Delete(user).Error
}
